//
//  HYFNewFeatureController.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/4.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "HYFNewFeatureController.h"
#import "HYFTabBarController.h"
#import "HYFAccount.h"
#import "HYFAccountTool.h"
#import "HYFOAuthController.h"
#import "AppDelegate.h"
#import "HYFWindowTool.h"


@interface HYFNewFeatureController ()<UIScrollViewDelegate>

@property (weak, nonatomic) UIPageControl *pageControl;


@end

@implementation HYFNewFeatureController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    
    NSInteger count = 4;
    
    for (int i = 0; i < count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]init];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%d",i + 1]];
        
        imageView.size = scrollView.size;
        imageView.x = i * scrollView.width;
        
        [scrollView addSubview:imageView];
        
        if (i == count - 1) {
            
            [self setupLastImageView:imageView];
        }
        
        
    }
    
    //设置scrollView 的内容大小
    [scrollView setContentSize:CGSizeMake(count * scrollView.width, 0)];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    
    //监听滑动,成为代理
    scrollView.delegate = self;
    
    
    [self.view addSubview:scrollView];
    
    //添加pageControl
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    self.pageControl = pageControl;
    
    pageControl.centerX = self.view.width * 0.5;
    pageControl.y = self.view.height - 100;
    
    //自动切掉多余的部分
//    pageControl.clipsToBounds = YES;
    
    pageControl.numberOfPages = count;
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    [self.view addSubview:pageControl];
    
}


//设置最后一个imageView
- (void)setupLastImageView:(UIImageView *)imageView{
    
    //打开用户交互
    imageView.userInteractionEnabled = YES;
    
    UIButton *enterButton = [[UIButton alloc]init];
    
    [enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateNormal];

    [enterButton setTitle:@"进入微博" forState:UIControlStateNormal];
    
    enterButton.size = enterButton.currentBackgroundImage.size;
    
    enterButton.centerX = imageView.width * 0.5;
    enterButton.y = imageView.height - 150;
    
    [enterButton addTarget:self action:@selector(enterButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    
    [imageView addSubview:enterButton];
    
    UIButton *shareButton = [[UIButton alloc]init];
    
    [shareButton setTitle:@"分享到微博" forState:UIControlStateNormal];
    
    [shareButton setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    
    [shareButton setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    
    [shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [shareButton sizeToFit];
    
    [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    shareButton.centerX = enterButton.centerX;
    shareButton.y = enterButton.y - 40;
    
    [imageView addSubview:shareButton];
}

//分享按钮点击
- (void)shareButtonClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
}

//进入微博按钮点击
- (void)enterButtonClick:(UIButton *)button{
    
    //判断是否有登录过
//    HYFAccount *account = [HYFAccountTool account];
//    //取出 appDelegate 里面的 window
//    UIWindow *window = ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
    
    [HYFWindowTool switchWindowWithWindow];
    
//    if (account) {
//        window.rootViewController = [[HYFTabBarController alloc]init];
//        
//    }else{
//        window.rootViewController = [[HYFOAuthController alloc]init];
//    }
}

#pragma mark - 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //计算滑动到第几页
    double page = scrollView.contentOffset.x / scrollView.width;
    //四舍五入
    self.pageControl.currentPage = (int)(page + 0.5);
    
}


@end
